package de.jmda.gen.java.impl;

import javax.validation.constraints.NotNull;

import de.jmda.gen.java.DeclaredElementGenerator;
import de.jmda.gen.java.DeclaredMethodGenerator;
import de.jmda.gen.java.MethodDeclarationGenerator;
import de.jmda.gen.java.MethodModifiersGenerator;
import de.jmda.gen.java.ModifiersUtil.VisibilityModifier;

/**
 * @author roger.jmda@gmail.com
 */
public class DefaultMethodDeclarationGenerator
		extends AbstractDeclarationGenerator
		implements MethodDeclarationGenerator
{
	private DeclaredMethodGenerator declaredMethodGenerator;

	public DefaultMethodDeclarationGenerator()
  {
		this(null);
  }

	/**
	 * @param generator
	 */
	public DefaultMethodDeclarationGenerator(
			@NotNull DeclaredMethodGenerator generator)
  {
	  super();

	  if (generator == null)
	  {
	  	generator = new DefaultDeclaredInstanceMethodGenerator();
	  }

	  setDeclaredMethodGenerator(generator);

	  setComponentSeparator(System.lineSeparator());
  }

	@Override
  public DeclaredMethodGenerator getDeclaredMethodGenerator()
  {
	  return declaredMethodGenerator;
  }

	@Override
  public void setDeclaredMethodGenerator(DeclaredMethodGenerator generator)
  {
		if (generator == null)
		{
			return;
		}
		
		this.declaredMethodGenerator = generator;
  }

	@Override
  public DeclaredElementGenerator getDeclaredElementGenerator()
  {
	  return getDeclaredMethodGenerator();
  }

	@Override
  public void addAnnotation(@NotNull Class<?> annotation)
  {
		demandAnnotationStatementsGenerator()
				.getAnnotationStatementGenerators()
				.add(new DefaultAnnotationStatementGenerator(annotation));
  }

	@Override
  public void setTypeName(@NotNull Class<?> type)
  {
		getDeclaredMethodGenerator().setTypeNameGenerator(
				new DefaultTypeNameGenerator(type.getName()));
  }

	@Override
  public void addParameter(@NotNull Class<?> type, @NotNull String name)
  {
		getDeclaredMethodGenerator().add(
				new DefaultParameterGenerator(type.getName(), name));
  }

	@Override
  public void setMethodBody(String methodBody)
  {
		getDeclaredMethodGenerator().setMethodBody(methodBody);
  }

	@Override
  public void setMethodName(String methodName)
  {
		getDeclaredMethodGenerator().setMethodNameGenerator(
				new DefaultMethodNameGenerator(methodName));
  }

	@Override
  public void setVisibility(VisibilityModifier modifier)
  {
		MethodModifiersGenerator modifiersGenerator =
				getDeclaredMethodGenerator().demandModifiersGenerator();
		
		if (modifier == VisibilityModifier.DEFAULT)
		{
			// do nothing
		}
		else if (modifier == VisibilityModifier.PRIVATE)
		{
			modifiersGenerator.setPrivate(true);
		}
		else if (modifier == VisibilityModifier.PROTECTED)
		{
			modifiersGenerator.setProtected(true);
		}
		else if (modifier == VisibilityModifier.PUBLIC)
		{
			modifiersGenerator.setPublic(true);
		}
  }
}